package com.tan.lbs.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tan.lbs.dao.MobileUserDAO;
import com.tan.lbs.dao.impl.MobileUserDAOImpl;
import com.tan.lbs.entity.MobileUser;

/**
 * Servlet implementation class MobileUserService
 */
@WebServlet("/MobileUserService")
public class MobileUserService extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MobileUserDAO mobileUserDAO;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MobileUserService() {
		super();
		mobileUserDAO = new MobileUserDAOImpl();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// request.getP
		String methodRequest = request.getParameter("method");
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		try {

			if ("login".equals(methodRequest)) {
				String username = request.getParameter("username");
				String password = request.getParameter("password");
				MobileUser user = mobileUserDAO.readByUserName(username);

				if (user != null) {
					if (user.getPasswd().equals(password))
						out.print("{message:true,userID:"+user.getUserID()+"}");
				} else {
					out.print("{message:false}");
				}
				out.flush();
			}
		} catch (Exception e) {
			out.write("{message:exception}");
			out.flush();
		}

	}

}
